<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Payment_log_entity{
	var $current_dbprefix = "";
	var $CI;
	var $id = 0;
	var $escort_id = 0;
	var $user_type = '';
	var $payment = '';
	var $reg_date = 0;
	var $apply_date = 0;
	var $user_id = 0;
	var $latest_payment_date = 0;
	var $payment_status= '';
	var $period = 0;
	var $discount = 0;
	var $service_type = '';
	var $type_id = 0;
	var $latest_period = 0;
	var $payment_price = 0;
	var $net_price = 0;
	var $using_flag = 'yes';
	var $discount_period_price = 0;
	var $discount_promo_price = 0;
	var $discount_volumn_price = 0;
	
	function __construct() {
		$this->CI= & get_instance();
		$this->current_dbprefix = $this->CI->db->dbprefix;
		$this->CI->db->set_dbprefix('default_');
	}
	function __destruct() {
		$this->CI->db->set_dbprefix($this->current_dbprefix);
	}
	function init($cmd, $input)
	{
		$this->CI->db->set_dbprefix('default_');
		
		if($cmd == 'id')
		{
			$result = $this->CI->db->where('id',$input)->get(TBL_ESCORT_PAYMENT_LOG)->result();
		}
		else
		{
			return false;
		}
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->escort_id = $result[0]->escort_id;
			$this->user_type = $result[0]->user_type;
			$this->payment = $result[0]->payment;
			$this->reg_date = $result[0]->reg_date;
			$this->apply_date = $result[0]->apply_date;
			$this->user_id = $result[0]->user_id;
			$this->latest_payment_date = $result[0]->latest_payment_date;
			$this->payment_status = $result[0]->payment_status;
			$this->period = $result[0]->period;
			$this->discount = $result[0]->discount;
			$this->service_type = $result[0]->service_type;
			$this->type_id = $result[0]->type_id;
			$this->latest_period = $result[0]->latest_period;
			$this->payment_price = $result[0]->payment_price;
			$this->net_price = $result[0]->net_price;
			$this->using_flag = $result[0]->using_flag;
			$this->discount_period_price = $result[0]->discount_period_price;
			$this->discount_promo_price = $result[0]->discount_promo_price;
			$this->discount_volumn_price = $result[0]->discount_volumn_price;
			
			return $this;
		}
		return false;
	}
	
	function setEscortId($input){$this->escort_id=$input;}
	function setUserType($input){$this->user_type=$input;}
	function setPayment($input){$this->payment=$input;}	 
	function setRegDate($input){$this->reg_date=$input;}
	function setApplyDate($input){$this->apply_date=$input;}
	function setUserId($input){$this->user_id=$input;}
	function setLatestPaymentDate($input){$this->latest_payment_date=$input;}
	function setPaymentStatus($input){$this->payment_status=$input;}
	function setPeriod($input){$this->period=$input;}
	function setDiscount($input){$this->discount=$input;}
	function setServiceType($input){$this->service_type=$input;}
	function setTypeId($input){$this->type_id=$input;}
	function setLatestPeriod($input){$this->latest_period=$input;}
	function setPaymentPrice($input){$this->payment_price=$input;}
	function setNetPrice($input){$this->net_price=$input;}
	function setUsingFlag($input){$this->using_flag=$input;}  
	function setDiscountPeriodPrice($input){$this->discount_period_price=$input;}
	function setDiscountPromoPrice($input){$this->discount_promo_price=$input;}
	function setDiscountVolumnPrice($input){$this->discount_volumn_price=$input;}
	
	function getId(){return $this->id;}
	function getEscortId(){return $this->escort_id;}
	function getUserType(){return $this->user_type;}
	function getPayment(){return $this->payment;}	 
	function getRegDate(){return $this->reg_date;}
	function getApplyDate(){return $this->apply_date;}
	function getUserId(){return $this->user_id;}
	function getLatestPaymentDate(){return $this->latest_payment_date;}
	function getPaymentStatus(){return $this->payment_status;}
	function getPeriod(){return $this->period;}
	function getDiscount(){return $this->discount;}
	function getServiceType(){return $this->service_type;}
	function getTypeId(){ return $this->type_id;}
	function getLatestPeriod(){return $this->latest_period;}
	function getPaymentPrice(){return $this->payment_price;}
	function getNetPrice(){return $this->net_price;}
	function getUsingFlag(){return $this->using_flag;}  
	function getDiscountPeriodPrice(){return $this->discount_period_price;}
	function getDiscountPromoPrice(){return $this->discount_promo_price;}
	function getDiscountVolumnPrice(){return $this->discount_volumn_price;}
	
	function update()
	{
		$this->CI->db->set_dbprefix('default_');
		
		$this->CI->db->set('escort_id', $this->escort_id);
		$this->CI->db->set('user_type', $this->user_type);
		$this->CI->db->set('payment', $this->payment);
		$this->CI->db->set('reg_date', $this->reg_date);
		$this->CI->db->set('apply_date', $this->apply_date);
		$this->CI->db->set('user_id', $this->user_id);
		$this->CI->db->set('latest_payment_date', $this->latest_payment_date);
		$this->CI->db->set('payment_status', $this->payment_status);
		$this->CI->db->set('discount', $this->discount);
		$this->CI->db->set('service_type', $this->service_type);
		$this->CI->db->set('latest_period', $this->latest_period);
		$this->CI->db->set('payment_price', $this->payment_price);
		$this->CI->db->set('type_id', $this->type_id);
		$this->CI->db->set('period', $this->period);
		$this->CI->db->set('net_price', $this->net_price);
		$this->CI->db->set('using_flag', $this->using_flag);
		$this->CI->db->set('discount_period_price', $this->discount_period_price);
		$this->CI->db->set('discount_promo_price', $this->discount_promo_price);
		$this->CI->db->set('discount_volumn_price', $this->discount_volumn_price);
		
		$this->CI->db->where('id', $this->id)->update(TBL_ESCORT_PAYMENT_LOG);
	}
	
	function createNewEntity(){
		$this->CI->db->set_dbprefix('default_');
		
		$this->CI->db->set('reg_date',time() );
		$this->CI->db->insert(TBL_ESCORT_PAYMENT_LOG);
		return $this->init('id',$this->CI->db->insert_id());
	}
	
	function getNextPaymentDate($user_id){
		$this->CI->db->set_dbprefix('default_');
		
		$rs = $this->CI->db->query("SELECT MIN(apply_date) AS apply_date FROM ".TBL_ESCORT_PAYMENT_LOG." WHERE user_id=$user_id AND using_flag='yes' ")->result();
		if($rs){
			return $rs[0]->apply_date;
		}
	}
	
	function getContractTimeExceed(){
		$this->CI->db->set_dbprefix('default_');
		
		$rs = $this->CI->db->query("SELECT * FROM ".TBL_ESCORT_PAYMENT_LOG." WHERE payment_status='paid' AND using_flag='yes' AND apply_date <".time())->result();
		return $rs;
	}
	
	function checkForeverPackage($user_id, $pack_id){
		$this->CI->db->set_dbprefix('default_');
		
		$rs = $this->CI->db->query(	" SELECT * FROM ".TBL_ESCORT_PAYMENT_LOG." WHERE user_id=$user_id AND type_id=$pack_id
									AND service_type='PACK' AND using_flag='yes' AND ( payment_status = 'paid' OR  payment_status = 'pending' )
									")->result();
		if($rs){
			return $rs[0]->id;
		}
		return false; 
	}
	
	function getServiceTypeStringIdsCurrently($user_id, $type){
		$this->CI->db->set_dbprefix('default_');
		
		$rs = $this->CI->db->query("SELECT * FROM ".TBL_PAYMENT_HISTORY." WHERE user_id=$user_id ORDER BY datetime DESC LIMIT 1")->result();
		if($rs){
			$contractLogIdArray = explode(',',$rs[0]->payment_ids);
			$idArr = array();
			foreach($contractLogIdArray as $item){
				$PAYLOG = $this->init('id', $item);
				if($PAYLOG->service_type == $type){
					$idArr[] = $PAYLOG->type_id;
				}
			}
			return implode(',',$idArr);
			/**
			$rs =  $this->CI->db->query( "SELECT * FROM ".TBL_ESCORT_PAYMENT_LOG.
					" WHERE escort_id=$escort_id AND service_type ='$type' AND using_flag='yes' 
					AND ( payment_status = 'paid' OR  payment_status = 'pending' )" )->result();
				
			$type_array = array();
			foreach($rs as $item){
				$type_array[] = $item->type_id;
			}	
			return implode(',',$type_array);
			**/
		}
		return '';
	}
	
	function getServicePeriodCurrently($user_id){
		$this->CI->db->set_dbprefix('default_');
		
		$rs = $this->CI->db->query("SELECT * FROM ".TBL_PAYMENT_HISTORY." WHERE user_id=$user_id ORDER BY datetime DESC LIMIT 1")->result();
		if($rs){
			return $rs[0]->period;
		}
		return 1;
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	
	function checkServiceExisted($escort_id, $type, $type_id){
		$this->CI->db->set_dbprefix('default_');
		
		$rs = $this->CI->db->where(array('escort_id'=>$escort_id, 'service_type'=>$type, 'type_id'=>$type_id, 'using_flag'=> 'yes'))
			->get(TBL_ESCORT_PAYMENT_LOG)->result();
		return ($rs) ? $this->init('id',$rs[0]->id) : false;	
	}
	
	function deleteServices($escort_id, $type, $type_id_array){
		$this->CI->db->set_dbprefix('default_');
		
		$rs =  $this->CI->db->where(array('escort_id'=>$escort_id, 'service_type'=>$type ))
			->get(TBL_ESCORT_PAYMENT_LOG)->result();
		$type_array = array();
		foreach($rs as $item){
			$type_array[] = $item->type_id;
		}	
		
		foreach($type_array as $id){
			if(!in_array($id, $type_id_array)){
				$this->CI->db->where( array('type_id'=>$id, 'service_type'=>$type, 'escort_id'=>$escort_id) )->delete(TBL_ESCORT_PAYMENT_LOG);
			}
		}
	}
	
	function getCurrentService($escort_id){
		$this->CI->db->set_dbprefix('default_');
		
		$rs = $this->CI->db->where(array('escort_id'=>$escort_id, 'using_flag'=>'yes'))
			->get(TBL_ESCORT_PAYMENT_LOG)->result();
		
		$price = 0;
		foreach($rs as $item){
			$price += $item->payment_price;
		}	
		return number_format($price,2);
	}
	
	function getCurrentServicePaid($escort_id){
		$this->CI->db->set_dbprefix('default_');
		
		$rs = $this->CI->db->where(array('escort_id'=>$escort_id, 'payment_status'=>'paid', 'using_flag'=>'yes'))
			->get(TBL_ESCORT_PAYMENT_LOG)->result();
		
		$price = 0;
		foreach($rs as $item){
			$price += $item->payment_price;
		}	
		return number_format($price,2);
	}
	
	function getNetPriceAmount($escort_id){
		$this->CI->db->set_dbprefix('default_');
		
		$rs = $this->CI->db->where(array('escort_id'=>$escort_id, 'payment_status'=>'paid', 'using_flag'=>'yes'))
			->get(TBL_ESCORT_PAYMENT_LOG)->result();
		
		$price = 0;
		foreach($rs as $item){
			$price += $item->net_price;
		}	
		return number_format($price,2);
	}
	
	function getServicesType($escort_id, $type){
		$this->CI->db->set_dbprefix('default_');
		
		$rs =  $this->CI->db->where(array('escort_id'=>$escort_id, 'service_type'=>$type, 'using_flag'=>'yes' ))
			->get(TBL_ESCORT_PAYMENT_LOG)->result();
		$type_array = array();
		foreach($rs as $item){
			$type_array[] = $item->type_id;
		}	
		return implode(',',$type_array);
	}
	
	function getCurrentServiceUnPaid($escort_id){
		$this->CI->db->set_dbprefix('default_');
		
		$rs = $this->CI->db->where(array('escort_id'=>$escort_id, 'payment_status'=>'unpaid', 'using_flag'=>'yes' ))
			->get(TBL_ESCORT_PAYMENT_LOG)->result();
		
		$price = 0;
		foreach($rs as $item){
			$price += $item->payment_price;
		}	
		return number_format($price,2);
	}
	
	function getNetAmount($escort_id){
		$this->CI->db->set_dbprefix('default_');
		
		$rs = $this->CI->db->where(array('escort_id'=>$escort_id, 'payment_status'=>'paid', 'using_flag'=> 'yes'))
			->get(TBL_ESCORT_PAYMENT_LOG)->result();
		
		$price = 0;
		foreach($rs as $item){
			$price += $item->net_price;
		}	
		return number_format($price,2);
	}
	
	function getServicesTypePaid($escort_id, $type){
		$this->CI->db->set_dbprefix('default_');
		
		$rs =  $this->CI->db->where(array('escort_id'=>$escort_id, 'service_type'=>$type, 'payment_status'=>'paid', 'using_flag'=> 'yes'))
			->get(TBL_ESCORT_PAYMENT_LOG)->result();
		$type_array = array();
		foreach($rs as $item){
			$type_array[] = $item->type_id;
		}	
		return implode(',',$type_array);
	}
	
	function getServicesTypeUnPaid($escort_id, $type){
		$this->CI->db->set_dbprefix('default_');
		
		$rs =  $this->CI->db->where(array('escort_id'=>$escort_id, 'service_type'=>$type, 'payment_status'=>'unpaid', 'using_flag'=> 'yes'))
			->get(TBL_ESCORT_PAYMENT_LOG)->result();
		$type_array = array();
		foreach($rs as $item){
			$type_array[] = $item->type_id;
		}	
		return implode(',',$type_array);
	}
	
	function getNetAmountOfEscort($escort_id){
		$this->CI->db->set_dbprefix('default_');
		
		$ESCORT = $this->CI->escort_entity->init('id',$escort_id);
		$DISCOUNT= $this->CI->discount_ticket_entity->getDiscountEscortByPromotionCode($ESCORT->getPromotionCode(),$ESCORT->getId());
		
		$rs = $this->CI->db->where(array('escort_id'=>$escort_id,'payment_status'=>'unpaid', 'using_flag'=> 'yes'))->get(TBL_ESCORT_PAYMENT_LOG)->result();
		$net_amount = 0;
		
		if($DISCOUNT){
			$amount = $DISCOUNT->amount;
			switch($DISCOUNT->unit){
				case 'month':
					$unit = 'MONTH';
					break;
				case '%':
					$unit = 'PERCENT';
					break;
				default:
					$unit = 'EURO';
					break;
			}
			
			foreach($rs as $item){
				if($unit == 'MONTH'){
					$tmp = $item->payment_price - ( $item->payment_price * $amount)/$item->latest_period;
					if($tmp >= 0){
						$net_amount += $tmp;
					}	
				}
				if($unit == 'PERCENT'){
					$tmp = $item->payment_price * ( 1 - $amount/100 );
					if($tmp >= 0){
						$net_amount += $tmp;
					}	
				}
				if($unit == 'EURO'){
					$tmp = $item->payment_price - $amount;
					if($tmp >= 0){
						$net_amount += $tmp;
					}	
				}
			}	
		}else{
			foreach($rs as $item){
				$net_amount += $item->payment_price;
			}	
			$unit = $amount = '';
		}
		return array(
			'net_amount'=> number_format($net_amount,2), 'discount_unit'=>$unit, 
			'discount_amount'=>$amount , 'promotion_code'=>$ESCORT->getPromotionCode()
		);
	}
	
	
	/////////////////////////////////////////////////FUNCTION OF AGENT/////////////////////////////////
	
	function checkServiceExisted_Agent($user_id, $type, $type_id){
		$this->CI->db->set_dbprefix('default_');
		
		$rs = $this->CI->db->where(array('user_id'=>$user_id, 'service_type'=>$type, 'type_id'=>$type_id, 'using_flag'=> 'yes'))
			->get(TBL_ESCORT_PAYMENT_LOG)->result();
		return ($rs) ? $this->init('id',$rs[0]->id) : false;	
	}
	
	function deleteServices_Agent($user_id, $type, $type_id_array){
		$this->CI->db->set_dbprefix('default_');
		
		$rs =  $this->CI->db->where(array('user_id'=>$user_id, 'service_type'=>$type ))
			->get(TBL_ESCORT_PAYMENT_LOG)->result();
		$type_array = array();
		foreach($rs as $item){
			$type_array[] = $item->type_id;
		}	
		
		foreach($type_array as $id){
			if(!in_array($id, $type_id_array)){
				$this->CI->db->where( array('type_id'=>$id, 'service_type'=>$type, 'user_id'=>$user_id) )
						->delete(TBL_ESCORT_PAYMENT_LOG);
			}
		}
	}
	
	function getCurrentService_Agent($user_id){
		$this->CI->db->set_dbprefix('default_');
		
		$rs = $this->CI->db->where(array('user_id'=>$user_id, 'using_flag'=> 'yes'))
			->get(TBL_ESCORT_PAYMENT_LOG)->result();
		
		$price = 0;
		foreach($rs as $item){
			$price += $item->net_price;
		}	
		return number_format($price,2);
	}
	
	function getCurrentServicePaid_Agent($user_id){
		$this->CI->db->set_dbprefix('default_');
		
		$rs = $this->CI->db->where(array('user_id'=>$user_id, 'payment_status'=>'paid', 'using_flag'=> 'yes'))
			->get(TBL_ESCORT_PAYMENT_LOG)->result();
		
		$price = 0;
		foreach($rs as $item){
			$price += $item->payment_price;
		}	
		return number_format($price,2);
	}
	
	function getServicesType_Agent($user_id, $type){
		$this->CI->db->set_dbprefix('default_');
		
		$rs =  $this->CI->db->where(array('user_id'=>$user_id, 'service_type'=>$type, 'using_flag'=> 'yes' ))
			->get(TBL_ESCORT_PAYMENT_LOG)->result();
		$type_array = array();
		foreach($rs as $item){
			$type_array[] = $item->type_id;
		}	
		return implode(',',$type_array);
	}
	
	function getCurrentServiceUnPaid_Agent($user_id){
		$this->CI->db->set_dbprefix('default_');
		
		$rs = $this->CI->db->where(array('user_id'=>$user_id, 'payment_status'=>'unpaid', 'using_flag'=> 'yes'))
			->get(TBL_ESCORT_PAYMENT_LOG)->result();
		
		$price = 0;
		foreach($rs as $item){
			$price += $item->payment_price;
		}	
		return number_format($price,2);
	}
	
	function getNetAmount_Agent($user_id){
		$this->CI->db->set_dbprefix('default_');
		
		$rs = $this->CI->db->where(array('user_id'=>$user_id, 'payment_status'=>'paid', 'using_flag'=> 'yes'))
			->get(TBL_ESCORT_PAYMENT_LOG)->result();
		
		$price = 0;
		foreach($rs as $item){
			$price += $item->net_price;
		}	
		return number_format($price,2);
	}
	
	function getServicesTypePaid_Agent($user_id, $type){
		$this->CI->db->set_dbprefix('default_');
		
		$rs =  $this->CI->db->where(array('user_id'=>$user_id, 'service_type'=>$type, 'payment_status'=>'paid', 'using_flag'=> 'yes'))
			->get(TBL_ESCORT_PAYMENT_LOG)->result();
		$type_array = array();
		foreach($rs as $item){
			$type_array[] = $item->type_id;
		}	
		return implode(',',$type_array);
	}
	
	function getServicesTypeUnPaid_Agent($user_id, $type){
		$this->CI->db->set_dbprefix('default_');
		
		$rs =  $this->CI->db->where(array('user_id'=>$user_id, 'service_type'=>$type, 'payment_status'=>'unpaid', 'using_flag'=> 'yes'))
			->get(TBL_ESCORT_PAYMENT_LOG)->result();
		$type_array = array();
		foreach($rs as $item){
			$type_array[] = $item->type_id;
		}	
		return implode(',',$type_array);
	}
	
	//deprecated
	function getNetAmountOfAgent($user_id, $number_escorts){
		$this->CI->db->set_dbprefix('default_');
		
		$AGENT_DISCOUNT = $this->CI->volume_discount_entity->getDiscountOfAgent($user_id, $number_escorts);
		
		$rs = $this->CI->db->where(array('user_id'=>$user_id,'payment_status'=>'unpaid'))->get(TBL_ESCORT_PAYMENT_LOG)->result();
		$net_amount = 0;
		
		if($AGENT_DISCOUNT){
			$amount = $AGENT_DISCOUNT->getDiscountAmountPerRegister();
			switch($AGENT_DISCOUNT->getDiscountUnit()){
				case '%':
					$unit = 'PERCENT';
					break;
				default:
					$unit = 'EURO';
					break;
			}
			
			foreach($rs as $item){
				if($unit == 'PERCENT'){
					$tmp = $item->payment_price * ( 1 - $amount/100 );
					if($tmp >= 0){
						$net_amount += $tmp;
					}	
				}
				if($unit == 'EURO'){
					$tmp = $item->payment_price - $amount;
					if($tmp >= 0){
						$net_amount += $tmp;
					}	
				}
			}	
		}else{
			foreach($rs as $item){
				$net_amount += $item->payment_price;
			}	
			$unit = $amount = '';
		}
		return array(
			'net_amount'=> number_format($net_amount,2), 'discount_unit'=>$unit, 
			'discount_amount'=>$amount 
		);
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////
	//deprecated
	function findObjPaymentOfEscort($escort_id, $timestamp){
		$this->CI->db->set_dbprefix('default_');
		
		list($from , $to) = array_values(getTimeStampMonthOfCurrentTime($timestamp));
		$rs = $this->CI->db->query("SELECT * FROM ".TBL_ESCORT_PAYMENT_LOG." WHERE escort_id=$escort_id AND apply_for_month>=$from AND apply_for_month<=$to")->result();
		if(!$rs) return false;
		return clone_( $this->init('id',$rs[0]->id) );
	}
	
	/////////////////////////////////////////////////CALCULATE COMISSION/////////////////////////////////////////////////
	function getArrayCountry_userManager($country_id){
		$this->CI->db->set_dbprefix('default_');
		
		$query = "SELECT A.* FROM ".TBL_ESCORT_USER_ROLES_N_BENEFITS." AS A INNER JOIN ".
					TBL_ESCORT_USER_ROLES_N_ZONES." AS B ON A.username=B.username 
					INNER JOIN default_groups AS C ON A.group=C.id 
					WHERE C.name = 'country-manager' AND B.country_id=$country_id AND A.status =1
					";
		$rs = $this->CI->db->query($query)->result();	
		return (!empty($rs)) ? $rs:false;	
	}
	
	function getArrayProvince_userManager($country_id,$state_id){
		$this->CI->db->set_dbprefix('default_');
		
		$query = "SELECT A.* FROM ".TBL_ESCORT_USER_ROLES_N_BENEFITS." AS A INNER JOIN ".
					TBL_ESCORT_USER_ROLES_N_ZONES." AS B ON A.username=B.username 
					INNER JOIN default_groups AS C ON A.group=C.id 
					WHERE C.name = 'province-manager' AND B.country_id=$country_id AND B.state_id=$state_id AND A.status =1
					";
		$rs = $this->CI->db->query($query)->result();	
		return (!empty($rs)) ? $rs:false;
	}
	
	function getArrayCity_userManager($country_id,$state_id,$city_id){
		$this->CI->db->set_dbprefix('default_');
		
		$query = "SELECT A.* FROM ".TBL_ESCORT_USER_ROLES_N_BENEFITS." AS A INNER JOIN ".
					TBL_ESCORT_USER_ROLES_N_ZONES." AS B ON A.username=B.username 
					INNER JOIN default_groups AS C ON A.group=C.id 
					WHERE C.name = 'city-manager' AND B.country_id=$country_id AND B.state_id=$state_id AND B.city_id=$city_id AND A.status =1
					";
		$rs = $this->CI->db->query($query)->result();	
		return (!empty($rs)) ? $rs:false;
	}
	
	function calculate_comission($user_id,$invoice_id){
		//$USER = $this->CI->user_entity->init('id',$user_id);
		//$user_type = $USER->getPermissionSlug();
		$this->CI->db->set_dbprefix('default_');
		
		$listingPlaceRc =  $this->CI->db->where(array('user_id'=>$user_id, 'service_type'=>'PLACE', 'payment_status'=>'unpaid', 'using_flag'=> 'yes'))
			->get(TBL_ESCORT_PAYMENT_LOG)->result();
		if($listingPlaceRc){
			$totalPrice = 0;
			foreach($listingPlaceRc as $item){
				$listing_place_id = $item->type_id;
				$rc = $this->CI->db->where('id',$listing_place_id)->get(TBL_PLACE_LIST)->result();
				$totalPrice += $rc[0]->price;
			}
			$T_average = (float) $totalPrice/sizeof($listingPlaceRc);
			foreach($listingPlaceRc as $item){
				$listing_place_id = $item->type_id;
				$rc = $this->CI->db->where('id',$listing_place_id)->get(TBL_PLACE_LIST)->result();
				
				$country_id = $rc[0]->country_id;
				$state_id = $rc[0]->state_id;
				$province_id = $rc[0]->province_id;
				$city_id = $rc[0]->city_id;	
				$price = $rc[0]->price;
				
				$country_manager_array = $this->getArrayCountry_userManager($country_id);
				$province_manager_array = $this->getArrayProvince_userManager($country_id,$state_id);
				$city_manager_array = $this->getArrayCity_userManager($country_id,$state_id,$city_id);
			 
				$country_wallet_array = $country_comission_array = array();
				$province_wallet_array = $province_comission_array = array();
				$city_wallet_array = $city_comission_array = array();
				
				if(($country_manager_array) AND !($province_manager_array) AND !($city_manager_array)){
					foreach($country_manager_array as $country_item_record){
						$country_comission_array[$country_item_record->username] = number_format( $country_item_record->comission *$T_average/100 ,2);
						$country_wallet_array[$country_item_record->username] = $country_comission_array[$country_item_record->username];
					}
				}elseif(($country_manager_array) AND ($province_manager_array) AND !($city_manager_array)){
					foreach($country_manager_array as $country_item_record){
						$country_comission_array[$country_item_record->username] = number_format( $country_item_record->comission * $T_average/100 ,2);
						$total_wallet_sub = 0;
						foreach($province_manager_array as $province_item_record){
							$province_comission_array[$province_item_record->username] = number_format( ($country_comission_array[$country_item_record->username] * $province_item_record->comission /100 ),2);
							$province_wallet_array[$province_item_record->username] = $province_comission_array[$province_item_record->username];
							$total_wallet_sub += $country_comission_array[$country_item_record->username] - $province_wallet_array[$province_item_record->username];
						}
						$country_wallet_array[$country_item_record->username] = $total_wallet_sub ;
					}
				}elseif(($country_manager_array) AND ($province_manager_array) AND ($city_manager_array)){
					foreach($country_manager_array as $country_item_record){
						$country_comission_array[$country_item_record->username] = number_format( $country_item_record->comission * $T_average/100 ,2);
						$total_wallet_sub_country = 0;
						foreach($province_manager_array as $province_item_record){
							$province_comission_array[$province_item_record->username] = number_format( ($country_comission_array[$country_item_record->username] * $province_item_record->comission /100 ),2);
							$total_wallet_sub_province = 0;
							foreach($city_manager_array as $city_item_record){
								$city_comission_array[$city_item_record->username]  = number_format( ($province_comission_array[$province_item_record->username] * $city_item_record->comission /100 ),2);
								$city_wallet_array[$city_item_record->username] = $city_comission_array[$city_item_record->username];
								$total_wallet_sub_province += $province_comission_array[$province_item_record->username] - $city_comission_array[$city_item_record->username];
							}
							$province_wallet_array[$province_item_record->username] = $total_wallet_sub_province;
							$total_wallet_sub_country += $country_comission_array[$country_item_record->username] - $province_comission_array[$province_item_record->username];
						}
						$country_wallet_array[$country_item_record->username] = $total_wallet_sub_country  ;
					}
				}
			 	
				if($country_manager_array){
					foreach($country_manager_array as $country_item){
						if(isset($country_wallet_array[$country_item->username])){
							$this->CI->db->set('country_id',$country_id);
							$this->CI->db->set('state_id',$state_id);
							$this->CI->db->set('province_id',$province_id);
							$this->CI->db->set('city_id',$city_id);
							$this->CI->db->set('price',$price);
							$this->CI->db->set('username',$country_item->username);
							$this->CI->db->set('group_id',$country_item->group);
							$this->CI->db->set('wallet',$country_wallet_array[$country_item->username]);
							$this->CI->db->set('status','pending');
							$this->CI->db->set('invoice_id',$invoice_id);
							$this->CI->db->set('user_reg_service_id',$user_id);
							$this->CI->db->set('listing_place_id',$listing_place_id);
							$this->CI->db->set('created_date',time());
							$this->CI->db->insert(TBL_COMISSION_LOG);
						}
					}
				}
				
				if($province_manager_array){
					foreach($province_manager_array as $province_item){
						if(isset($province_wallet_array[$province_item->username])){
							$this->CI->db->set('country_id',$country_id);
							$this->CI->db->set('state_id',$state_id);
							$this->CI->db->set('province_id',$province_id);
							$this->CI->db->set('city_id',$city_id);
							$this->CI->db->set('price',$price);
							$this->CI->db->set('username',$province_item->username);
							$this->CI->db->set('group_id',$province_item->group);
							$this->CI->db->set('wallet',$province_wallet_array[$province_item->username]);
							$this->CI->db->set('status','pending');
							$this->CI->db->set('invoice_id',$invoice_id);
							$this->CI->db->set('user_reg_service_id',$user_id);
							$this->CI->db->set('listing_place_id',$listing_place_id);
							$this->CI->db->set('created_date',time());
							$this->CI->db->insert(TBL_COMISSION_LOG);
						}
					}
				}
				
				if($city_manager_array){
					foreach($city_manager_array as $city_item){
						if(isset($city_wallet_array[$city_item->username])){
							$this->CI->db->set('country_id',$country_id);
							$this->CI->db->set('state_id',$state_id);
							$this->CI->db->set('province_id',$province_id);
							$this->CI->db->set('city_id',$city_id);
							$this->CI->db->set('price',$price);
							$this->CI->db->set('username',$city_item->username);
							$this->CI->db->set('group_id',$city_item->group);
							$this->CI->db->set('wallet',$city_wallet_array[$city_item->username]);
							$this->CI->db->set('status','pending');
							$this->CI->db->set('invoice_id',$invoice_id);
							$this->CI->db->set('user_reg_service_id',$user_id);
							$this->CI->db->set('listing_place_id',$listing_place_id);
							$this->CI->db->set('created_date',time());
							$this->CI->db->insert(TBL_COMISSION_LOG);
						}
					}
				}
				 
			}
		}	
	}
	function updateStatus($ids, $status){
		$this->CI->db->set_dbprefix('default_');
		
		$this->CI->db->set('payment_status', $status);
		$this->CI->db->where_in('id', $ids)->update(TBL_ESCORT_PAYMENT_LOG);
	}
	
	//end class
}	